package com.dhl.deliver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhl.deliver.entity.Product;
import com.dhl.deliver.entity.User;
import com.dhl.deliver.service.FilePathService;
import com.dhl.deliver.service.IProductService;
import com.dhl.deliver.utils.GridResultWrapper;
import com.dhl.deliver.utils.Result;
import com.dhl.deliver.utils.ResultEnum;
import com.dhl.deliver.utils.ResultUtil;
import com.dhl.deliver.vo.GoodsVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dhl
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    IProductService productService;

    @ApiOperation("向商铺中添加商品")
    @PostMapping("/addProduct")
    public Result addProduct(String productName,double price,String introduce,Integer shop_id){
        Product product=new Product();
        product.setName(productName);
        product.setPrice(price);
        product.setIntroduce(introduce);
        product.setShopId(shop_id);
        if(productService.save(product))
            return ResultUtil.success();
        return ResultUtil.error(500,"添加失败");
    }

    @ApiOperation("获取所有商品名称，价格及描述,分页")
    @GetMapping("/getAllProducts")
    public Result getAllProducts(int pageNum, int size){
        IPage<Product> page=new Page<Product>(pageNum,size);
        IPage<Product> resultPage=productService.page(page);
        GridResultWrapper resultWrapper = new GridResultWrapper(resultPage.getTotal(),resultPage.getRecords() );
        Result result = ResultUtil.success(resultWrapper);
        return result;
    }

    @ApiOperation("根据商店获取商店所对应的商品名称、价格及描述，分页")
    @GetMapping("/getProductsByShopId")
    public Result getProductsByShopId(String shopId,int pageNum,int size) {
        QueryWrapper<Product> wrapper=new QueryWrapper<Product>();
        wrapper.eq("shop_id",shopId);
        List<Product> list = productService.list(wrapper);
        Result result=ResultUtil.success(list);
        return result;
    }

    @ApiOperation("根据商品id，获取商品的各个字段")
    @GetMapping("/getProductById")
    public Result getProductById(String id){
        Product product=productService.getById(id);
        Result result=ResultUtil.success(product);
        return result;
    }

    @ApiOperation("根据商品id,删除商品")
    @GetMapping("/deleteProductById")
    public Result deleteProductById(String id){
        if(productService.removeById(id)){
            Result result=ResultUtil.success(ResultEnum.SUCCESS);
            return result;
        }
        else{
            Result result=ResultUtil.success(ResultEnum.ERROR);
            return result;
        }
    }

    @Autowired
    private FilePathService filePathService;

    @ApiOperation("根据商品id，上传商品对应的图片")
    @PostMapping("/upload")
    public Result upload(String id,@RequestParam("file") MultipartFile file)
    {
        String path=filePathService.Upload(file);
        if(path=="fail")
            return ResultUtil.error(500,"上传失败");
        else {
            Product product=productService.getById(id);
            if(product!=null){
                product.setPicture(path);
                productService.updateById(product);
                return ResultUtil.success();
            }
            else
                return ResultUtil.error(500,"未找到指定商品");
        }
    }

    @Value("${domainName}")
    private String domainName;
    @ApiOperation("根据商品id，获取商品图片")
    @PostMapping("/getPicture")
    public Result getPicture(String id){
        Product product=productService.getById(id);
        if(product!=null){
            String picture=product.getPicture();
            return ResultUtil.success(domainName+picture);
        }
        else
            return ResultUtil.error(500,"未找到指指定");
    }
}
